#include "org_thinkinbunny_financial_encrypt_Encrypt.h"
#include "Des.h"
#include "BunnyEncrypt.h"
#include "stdio.h"
#include <iostream>
#include <string>
#include <android/log.h>
#include <stdlib.h> 
#include <unistd.h>
using namespace std;


#define LOG_TAG	"JNI_NDK"

#define GET_SIGNATURES 64

#define LENGTH_LONG	1

#define	LOGI(...)	__android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
//#define	LOGE(...)	__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
//#define	LOGI(...)	1
#define LOGE(...)	1
JNIEXPORT jstring JNICALL Java_org_thinkinbunny_financial_encrypt_Encrypt_getSHA1
  (JNIEnv *jniEnv, jobject, jstring input_jstring)
{
    string s = "";
    const char* pBuffer = jniEnv->GetStringUTFChars(input_jstring, NULL);
    s += pBuffer;
	const char HEX_CHAR[]={'0', '1', '2', '3', '4', '5', '6', '7',
		'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
	const unsigned long K[] = {0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6};
	//��չ��K*512λ
	unsigned long *data;
	int l;
	l = s.length()*8;
	data = new unsigned long[((l/512)+1)*512];
	memset(data, 0, sizeof(data[0])*((l/512)+1)*512);
	for(unsigned int i = 0; i < s.length(); ++i){
		data[i / 4] |= s[i] << 8*(3 - (i % 4));
	}
	data[s.length() / 4] |= 0x80 << 8*(3-(s.length()%4));
	data[((l/512)+1)*512/32-1]=l;
	l = (l/512)+1;
	//calculator
	unsigned long H[5], G[5];
	H[0] = G[0] = 0x67452301;
	H[1] = G[1] = 0xEFCDAB89;
	H[2] = G[2] = 0x98BADCFE;
	H[3] = G[3] = 0x10325476;
	H[4] = G[4] = 0xC3D2E1F0;
	for(int i = 0; i<l; ++i){
		unsigned long W[80];
		int t;
		for(t = 0; t<16; ++t)
			W[t] = data[i*16+t];
		for(t = 16; t<80; ++t){
			unsigned long tmp = W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16];
			W[t] = (tmp << 1)|(tmp >> 31);
		}
		unsigned long tmp;
		for(t = 0; t<5; ++t)
			H[t] = G[t];
		for(t = 0; t<20; ++t){
			tmp = ((H[0] << 5) | (H[0] >> 27)) + ((H[1] & H[2]) | (~ H[1] & H[3])) + H[4] + W[t] + K[0];
			H[4] = H[3]; H[3] = H[2]; H[2] = (H[1]<<30)|(H[1] >> 2); H[1] = H[0]; H[0] = tmp;
		}
		for(t = 20; t<40; ++t){
			tmp = ((H[0] << 5) | (H[0] >> 27)) + (H[1] ^ H[2] ^ H[3]) + H[4] + W[t] + K[1];
			H[4] = H[3]; H[3] = H[2]; H[2] = (H[1]<<30)|(H[1] >> 2); H[1] = H[0]; H[0] = tmp;
		}
		for(t = 40; t<60; ++t){
			tmp = ((H[0] << 5) | (H[0] >> 27)) + ((H[1] & H[2])|(H[2] & H[3])|(H[1] & H[3])) + H[4] + W[t] + K[2];
			H[4] = H[3]; H[3] = H[2]; H[2] = (H[1]<<30)|(H[1] >> 2); H[1] = H[0]; H[0] = tmp;
		}
		for(t = 60; t<80; ++t){
			tmp = ((H[0] << 5) | (H[0] >> 27)) + (H[1] ^ H[2] ^ H[3]) + H[4] + W[t] + K[3];
			H[4] = H[3]; H[3] = H[2]; H[2] = (H[1]<<30)|(H[1] >> 2); H[1] = H[0]; H[0] = tmp;
		}
		for(t = 0; t<5; ++t)
			G[t] += H[t];
	}
	delete data;
	char buf[41];
	for(int i = 0; i<40; ++i){
		buf[i] = HEX_CHAR[(G[i / 8] >> (4*(7- (i % 8))))&0xf];
	}
	buf[40] = '\0';
	return jniEnv->NewStringUTF(buf);
}

string getSHA(string s)
{
	const char HEX_CHAR[]={'0', '1', '2', '3', '4', '5', '6', '7',
		'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
	const unsigned long K[] = {0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6};
	//��չ��K*512λ
	unsigned long *data;
	int l;
	l = s.length()*8;
	data = new unsigned long[((l/512)+1)*512];
	memset(data, 0, sizeof(data[0])*((l/512)+1)*512);
	for(unsigned int i = 0; i < s.length(); ++i){
		data[i / 4] |= s[i] << 8*(3 - (i % 4));
	}
	data[s.length() / 4] |= 0x80 << 8*(3-(s.length()%4));
	data[((l/512)+1)*512/32-1]=l;
	l = (l/512)+1;
	//calculator
	unsigned long H[5], G[5];
	H[0] = G[0] = 0x67452301;
	H[1] = G[1] = 0xEFCDAB89;
	H[2] = G[2] = 0x98BADCFE;
	H[3] = G[3] = 0x10325476;
	H[4] = G[4] = 0xC3D2E1F0;
	for(int i = 0; i<l; ++i){
		unsigned long W[80];
		int t;
		for(t = 0; t<16; ++t)
			W[t] = data[i*16+t];
		for(t = 16; t<80; ++t){
			unsigned long tmp = W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16];
			W[t] = (tmp << 1)|(tmp >> 31);
		}
		unsigned long tmp;
		for(t = 0; t<5; ++t)
			H[t] = G[t];
		for(t = 0; t<20; ++t){
			tmp = ((H[0] << 5) | (H[0] >> 27)) + ((H[1] & H[2]) | (~ H[1] & H[3])) + H[4] + W[t] + K[0];
			H[4] = H[3]; H[3] = H[2]; H[2] = (H[1]<<30)|(H[1] >> 2); H[1] = H[0]; H[0] = tmp;
		}
		for(t = 20; t<40; ++t){
			tmp = ((H[0] << 5) | (H[0] >> 27)) + (H[1] ^ H[2] ^ H[3]) + H[4] + W[t] + K[1];
			H[4] = H[3]; H[3] = H[2]; H[2] = (H[1]<<30)|(H[1] >> 2); H[1] = H[0]; H[0] = tmp;
		}
		for(t = 40; t<60; ++t){
			tmp = ((H[0] << 5) | (H[0] >> 27)) + ((H[1] & H[2])|(H[2] & H[3])|(H[1] & H[3])) + H[4] + W[t] + K[2];
			H[4] = H[3]; H[3] = H[2]; H[2] = (H[1]<<30)|(H[1] >> 2); H[1] = H[0]; H[0] = tmp;
		}
		for(t = 60; t<80; ++t){
			tmp = ((H[0] << 5) | (H[0] >> 27)) + (H[1] ^ H[2] ^ H[3]) + H[4] + W[t] + K[3];
			H[4] = H[3]; H[3] = H[2]; H[2] = (H[1]<<30)|(H[1] >> 2); H[1] = H[0]; H[0] = tmp;
		}
		for(t = 0; t<5; ++t)
			G[t] += H[t];
	}
	delete data;
	char buf[41];
	for(int i = 0; i<40; ++i){
		buf[i] = HEX_CHAR[(G[i / 8] >> (4*(7- (i % 8))))&0xf];
	}
	buf[40] = '\0';
	return buf;
}

JNIEXPORT jstring JNICALL Java_org_thinkinbunny_financial_encrypt_Encrypt_encode
  (JNIEnv * jniEnv, jobject, jstring input_jstring)
{
    string input = "";
    const char* pBuffer = jniEnv->GetStringUTFChars(input_jstring, NULL);
    
    if (pBuffer != NULL)
    {
        input += pBuffer;
    }
    
    return jniEnv->NewStringUTF(getSHA(input).c_str());
}


JNIEXPORT jstring JNICALL Java_org_thinkinbunny_financial_encrypt_Encrypt_des
  (JNIEnv * jniEnv, jobject, jstring input_jstring)
{
    int key[]={1,2,3,4,5};//加密字符
	char s[]="FindClass";
	char *p=s;
	//cout<<"加密前:"<<p<<endl;
	Encrypt(s,key);//加密
	//cout<<"加密后:"<<p<<endl;    
	//Decrypt(s,key);//解密
	//cout<<"解密后:"<<p<<endl;
	LOGI("Java_org_thinkinbunny_financial_encrypt_Encrypt_des");
    LOGI(p);
    return jniEnv->NewStringUTF(p);
}
JNIEXPORT jstring JNICALL Java_org_thinkinbunny_financial_encrypt_Encrypt_desRes
  (JNIEnv * jniEnv, jobject, jstring input_jstring)
{
    int key[]={1,2,3,4,5};//加密字符
	char s[]="Bnbwv^<emke焹㔲";
	char *p=s;
	//cout<<"加密前:"<<p<<endl;
	//Encrypt(s,key);//加密
	//cout<<"加密后:"<<p<<endl;    
	Decrypt(s,key);//解密
	//cout<<"解密后:"<<p<<endl;
    // LOGI(s);
    	LOGI("Java_org_thinkinbunny_financial_encrypt_Encrypt_des");
    LOGI(p);
    return jniEnv->NewStringUTF(p);
}
JNIEXPORT jstring JNICALL Java_org_thinkinbunny_financial_encrypt_Encrypt_encode2
  (JNIEnv * jniEnv, jobject, jstring input_jstring)
{
	char key[]={0,2,0,0,9,3,5,1,9,8,0,0,9,1,7},buf[255];
	char str[]="aawfdfsefdsfdew";

	memset(buf, 0, sizeof(buf));
	strcpy(buf, str);
	//puts("\nBefore encrypting");
	//puts(buf);

	//Des_Go(buf, buf, sizeof(str), key, sizeof(key), ENCRYPT);
	//puts("\nAfter encrypting");
	//puts(buf);

Des_Go(buf, buf, sizeof(str), key, sizeof(key), DECRYPT);
	//puts("\nAfter decrypting");
	//puts(buf);
	

    
    return jniEnv->NewStringUTF(buf);
}
//char EncryptChar(char c,int key){
//	return c=c^key;
//}
//char EncryptChar(char c,int key){
//	return c=c^key;
//}



